package com.situ.mall.order.controller;

import com.situ.mall.base.entity.Member;
import com.situ.mall.base.exception.InfoException;
import com.situ.mall.base.service.IMemberService;
import com.situ.mall.base.util.URLUtil;
import com.situ.mall.base.vo.ResVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * ClassName: RegisterController
 * Package: com.situ.mall.order.controller
 * Description:
 *
 * @Author Darling_达
 * @Create 2023-03-24 17:28
 * @Version 1.0
 */

@Controller
@RequestMapping(URLUtil.REGISTER)
public class RegisterController {

    @Autowired
    private IMemberService memberService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping
    public String index(){
        return URLUtil.REGISTER_HTML;
    }


    @PostMapping(URLUtil.REGISTER)
    @ResponseBody
    public ResVO register(String tel, String code){
        ResVO vo = new ResVO();
        Object obj = redisTemplate.opsForValue().get(URLUtil.REG_CODE + tel);
        // 判断 code 是否正确
        if (obj == null || !((String) obj).equals(code)) {
            throw new InfoException("验证码输入不正确！", 500);
        }
        // 如果正确  插入一条 member 会员数据
        vo.setData("注册成功！");
        memberService.save(new Member(tel, "1111", tel,0,UUID.randomUUID().toString(),LocalDateTime.now()));
        return vo;
    }




}
